package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.*;
import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.mapper.OrderDetailMapper;
import com.zrrd.reggie.mapper.OrdersMapper;
import com.zrrd.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Override
    @Transactional
    public void submit(Orders orders) {
        //拿到用户id
        Long userId = ThreadLocalUtil.getCurrentId();
        //根据用户id查询用户的购物车数据
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        User user = userService.getById(userId);
        //获取地址信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        //生成订单号
        Long orderNumber = IdWorker.getId();
        //计算金额
        AtomicInteger amount =new AtomicInteger(0);
        //创建订单集合
        List<OrderDetail> orderDetailList =new ArrayList<>();
        for (ShoppingCart shoppingCart:shoppingCartList){
            OrderDetail detail = new OrderDetail();
            detail.setOrderId(orderNumber);
            detail.setNumber(shoppingCart.getNumber());
            detail.setDishId(shoppingCart.getDishId());
            detail.setDishFlavor(shoppingCart.getDishFlavor());
            detail.setSetmealId(shoppingCart.getSetmealId());
            detail.setName(shoppingCart.getName());
            detail.setImage(shoppingCart.getImage());
            detail.setAmount(shoppingCart.getAmount());
            //金额累乘
            amount.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
            //将对象加入集合
            orderDetailList.add(detail);
        }
        orderDetailService.saveBatch(orderDetailList);
        orders.setId(orderNumber);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(amount.get()));
        orders.setUserId(userId);
        orders.setNumber(orderNumber+"");
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        this.save(orders);
        //清空用户的购物车
        shoppingCartService.remove(wrapper);


    }

    @Override
    public void again(Orders orders) {
        //根据订单id查询订单明细
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orders.getId());
        List<OrderDetail> orderDetails = orderDetailService.list(wrapper);
        //新建购物车集合
        List<ShoppingCart> shoppingCartList =new ArrayList<>();
        //遍历明细集合
        for (OrderDetail detail : orderDetails) {
            ShoppingCart cart =new ShoppingCart();
            //属性复制
            BeanUtils.copyProperties(detail,cart);
            //对比表知道差一个
            cart.setUserId(ThreadLocalUtil.getCurrentId());
            //设置创建时间
            cart.setCreateTime(LocalDateTime.now());
            //将购物车对象添加购物车集合
            shoppingCartList.add(cart);
        }

        shoppingCartService.saveBatch(shoppingCartList);
    }
}
